<?php 
class User_model extends MY_Model {
    
    function __construct() {
        parent::__construct("c_user");
    }
    
	/**
     * 公用查询程序
     * $condition['category'] = ?  //按照用户类别搜索
     * $condition['keywords'] = ?  //按照用户名关键字查询
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE) {
        $this->db->select('id, name, password, email, category_id, create_time');

        if (isset($condition['category'])) {
            $this->db->where('category_id', $condition['category']);
        }
        
    	if (isset($condition['keywords'])) {
            $this->db->like('name', $condition['keywords']);
        }

        if (!$count_FLAG) {

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }

            }

            $query = $this->db->get('c_user');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('c_user');
            return $this->db->count_all_results();
        }
    }
    
    /**
     * 用户登录使用的函数 使用name 得到user数据
     * @param $name 
     * @param $category 1:社团  2:社联员工 3:社团指导老师 4:管理员
     */
    function get_user_by_name_and_category($name, $category) {
        $sql = "SELECT * FROM c_user WHERE name = ? AND category_id = ?";
        $query = $this->db->query($sql, array($name, $category));
        return $query->row_array();
    }
    
    function get_user_by_id($id) {
    	$sql = "SELECT id, name, email FROM c_user WHERE id = ? and category_id = 1";
        $query = $this->db->query($sql, array($id));
        return $query->row_array();
    }
    
	/**
	 * 通过name查询数据
	 * @param $name
	 */
	function get_user_by_name_single($name) {
		$sql = "SELECT * FROM c_user WHERE name = ?";
		$query = $this->db->query($sql, array($name));
		return $query->row_array();
	}
	
	function get_user_by_name_login($name) {
		$sql = "SELECT * FROM c_user WHERE name = ? AND category_id != '" . ADMIN_UCID . "'";
		$query = $this->db->query($sql, array($name));
		return $query->row_array();
	}
	
	function get_user_by_email_login($email) {
		$sql = "SELECT * FROM c_user WHERE email = ? AND category_id != '" . ADMIN_UCID . "'";
		$query = $this->db->query($sql, array($email));
		return $query->row_array();
	}
	
	function get_user_by_password_and_id($password, $id) {
		$sql = "SELECT * FROM c_user WHERE password = ? AND id = ?";
		$data = $this->db->query($sql, array($password, $id));
		return $data->result();
	}
	
	/**
	 * 通过email查询数据
	 * @param $email
	 */
	function get_user_by_email_single($email) {
		$sql = "SELECT * FROM c_user WHERE email = ?";
		$data = $this->db->query($sql, array($email));
		return $data->result();
	}
	
	function get_user_by_email_and_id($email, $id) {
		$sql = "SELECT * FROM c_user WHERE email = ? AND id = ?";
		$data = $this->db->query($sql, array($email, $id));
		return $data->result();
	}
	
	function _get_user_by_identifier($identifier, $select_key = '*') {
		$query = $this->db->query("SELECT " . $select_key . " FROM {$this->table_name} WHERE identifier = ?", array($identifier));
		$data = $query->row_array();
		if (!stripos($select_key, ',') && $select_key != '*') {
			return $data[$select_key];
		} else {
			return $data;
		}
	}

}